package com.insurance.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.insurance.dao.AuthenticationUserEnterpriseDao;
import com.insurance.entity.AuthenticationUserEnterpriseEntity;
import com.insurance.service.AuthenticationUserEnterpriseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.Query;



@Service("authenticationUserEnterpriseService")
public class AuthenticationUserEnterpriseServiceImpl extends ServiceImpl<AuthenticationUserEnterpriseDao, AuthenticationUserEnterpriseEntity> implements AuthenticationUserEnterpriseService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AuthenticationUserEnterpriseEntity> page = this.page(
                new Query<AuthenticationUserEnterpriseEntity>().getPage(params),
                new QueryWrapper<AuthenticationUserEnterpriseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 通过用户主键查询用户企业关联表
     *
     * @param userId 用户主键
     * @return
     */
    @Override
    public List<AuthenticationUserEnterpriseEntity> queryUserEnterpriseListByUserId(String userId){
        if(StringUtils.isBlank(userId)){
            return new ArrayList<AuthenticationUserEnterpriseEntity>();
        }
        QueryWrapper<AuthenticationUserEnterpriseEntity> queryWrapper = new QueryWrapper<AuthenticationUserEnterpriseEntity>();
        queryWrapper.eq("user_id", userId);
        return this.list(queryWrapper);
    }

    /**
     * 通过用户主键查询用户企业id集合
     *
     * @param userId 用户主键
     * @return
     */
    @Override
    public List<String> queryEnterpriseIdListByUserId(String userId){
        List<AuthenticationUserEnterpriseEntity> userEnterpriseList = this.queryUserEnterpriseListByUserId(userId);
        if(CollectionUtil.isEmpty(userEnterpriseList)){
            return new ArrayList<String>();
        }
        List<String> enterpriseIdList = userEnterpriseList.stream().map(AuthenticationUserEnterpriseEntity::getEnterpriseId).collect(Collectors.toList());
        return enterpriseIdList;
    }

}